package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.ChannelDiscountMgtExcution;
import com.fortune.dto.in.channelDiscount.ChannelDiscountAddDto;
import com.fortune.dto.in.channelDiscount.ChannelDiscountEditDto;
import com.fortune.dto.in.channelDiscount.ChannelDiscountListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.ChannelDiscountMgtEnum;
import com.fortune.service.ChannelDiscountMgtService;
import com.fortune.service.MProductInfoService;
import com.fortune.vo.channelDiscountMgt.ChannelDiscount;
import com.fortune.vo.channelDiscountMgt.ProductInChannealDiscountMgt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "channelDiscountMgt", description = "渠道优惠政策管理", tags = {"渠道优惠政策管理"})
@RestController
@RequestMapping("channelDiscountMgt")
public class ChannelDiscountMgtController {
	@Autowired
	private ChannelDiscountMgtService channelDiscountMgtService;
	@Autowired
	private MProductInfoService mProductInfoService;
	
	@ApiOperation(value = "优惠政策列表", nickname = "channelDiscountList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="order", value="排序依据(起始日期:start_time,截止日期:end_time,满数量:achieve_num,赠数量:gift_num,优惠名称:name,商品名称:produceName,商品编号:number,默认为start_time)",allowableValues = "start_time,end_time,achieve_num,gift_num,name,produceName,number",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
	@GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<ChannelDiscount>> list(HttpServletRequest request,@ApiIgnore @Validated ChannelDiscountListDto channelDiscountListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		channelDiscountListDto.setPage((channelDiscountListDto.getPage()-1)*10);
		List<ChannelDiscount> channelDiscountList = channelDiscountMgtService.queryChannelDiscountList(companyId,channelDiscountListDto);
		int count = channelDiscountMgtService.queryChannelDiscountListCount(companyId,channelDiscountListDto);
		PagingData<ChannelDiscount> pagingData = new PagingData<>();
		pagingData.setData(channelDiscountList);
		pagingData.setRecordsTotal(count);
		return new ChannelDiscountMgtExcution<PagingData<ChannelDiscount>>(pagingData, ChannelDiscountMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加优惠政策", nickname = "channelDiscountAdd", notes = "", tags={ "@石匡" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody ChannelDiscountAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = channelDiscountMgtService.queryChannelDiscountCountByName(companyId,addDto.getName());
		if (num > 0){
			return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.EXIST);
		}
		num = channelDiscountMgtService.addChannelDiscountCount(companyId,addDto);
		if (num < 1){
			return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.FAILURE);
		}
		return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除优惠政策", nickname = "channelDiscountDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="优惠政策id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="优惠政策id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = channelDiscountMgtService.deleteChannelDiscountCount(companyId,id);
		if (num < 1){
			return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.FAILURE);
		}
		return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑优惠政策", nickname = "channelDiscountEdit", notes = "", tags={ "@石匡" })
    @PostMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody ChannelDiscountEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = channelDiscountMgtService.queryIsExist(companyId,editDto);
		if (num > 0){
			return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.EXIST);
		}
		num = channelDiscountMgtService.editChannelDiscountCount(companyId,editDto);
		if (num < 1){
			return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.FAILURE);
		}
		return new ChannelDiscountMgtExcution<Void>(null, ChannelDiscountMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "商品列表", nickname = "channelDiscountProductList", notes = "", tags={ "@石匡" })
    @GetMapping(value = "productList",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ProductInChannealDiscountMgt>> productList(HttpServletRequest request) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		List<ProductInChannealDiscountMgt> list = mProductInfoService.queryProduceDiscount(companyId);
		return new ChannelDiscountMgtExcution<List<ProductInChannealDiscountMgt>>(list, ChannelDiscountMgtEnum.SUCCESS);
	}
}
